import Vue from 'vue'
import Vuex from 'vuex'
import Axios from 'axios'
Axios.defaults.withCredentials = true
import * as serviceApi from "../common/service.config"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    treeData: null,
    activeName: 'first',
    activeLabel: false,
    activeName2: '单点变形分析',
    activeName3: '单点变形分析',
    activeName4: '累计位移',
    baseId: 1,
    navId: 1,
    token: 24,
    timeType: 1,
    activeName5: '数据预警',
    projectInfo: null,
    activeName6: '区域分析',
    activeName7: '累计流量',
  },
  mutations: {
    setData (state, payload) {
      state.treeData = payload
    },
    setTabName (state, name) {
      state.activeName = name
    },
    setTabName2 (state, name) {
      state.activeName2 = name
    },
    setTabName3 (state, name) {
      state.activeName3 = name
    },
    setTabName4 (state, name) {
      state.activeName4 = name
    },
    setRadioLabel (state,label) {
      state.activeLabel = label
    },
    setChart (state,id) {
      state.baseId = id
    },
    setNav (state,id) {
      state.navId = id
    },
    setToken (state,token) {
      state.token = token
    },
    setTimeType (state,type) {
      state.timeType = type
    },
    setTabName5 (state, name) {
      state.activeName5 = name
    },
    setProjectInfo (state, payload) {
      state.projectInfo = payload
    },
    setTabName6 (state, name) {
      state.activeName6 = name
    },
    setTabName7 (state, name) {
      state.activeName7 = name
    },
  },
  actions: {
    getTabName ({ commit }, name) {
      commit('setTabName', name)
    },
    getTabName2 ({ commit }, name) {
      commit('setTabName2', name)
    },
    getTabName3 ({ commit }, name) {
      commit('setTabName3', name)
    },
    getTabName4 ({ commit }, name) {
      commit('setTabName4', name)
    },
    getRadioLabel ({commit}, label) {
      commit('setRadioLabel', label)
    },
    getChart ({commit}, id) {
      commit('setChart', id)
    },
    getNav ({commit}, id) {
      commit('setNav', id)
    },
    getToken ({commit}, token) {
      commit('setToken', token)
    },
    getTimeType ({commit}, type) {
      commit('setTimeType', type)
    },
    getTabName5 ({ commit }, name) {
      commit('setTabName5', name)
    },
    getData ({ commit }) {
      return new Promise((resolve, reject) => {
        Axios({
          url: serviceApi.KB_API,
          method: 'post',
          data: `appMsg={"parameter":"{}","serviceCode":"${serviceApi.CONSTANT.LIST_MONITOR_AND_SENSOR}","timestamp":"","uuid":""}`
        }).then(res => {
          commit('setData', res.data.parameter.list);
          resolve('success');
        }).catch(error => {
          reject(error);
        });
      })
    },
    getProjectInfo({commit}) {
      return new Promise((resolve, reject) => {
        Axios({
          url: serviceApi.KB_API,
          method: "post",
          data: `appMsg={"parameter":"{\\"id\\":\\"\\",\\"projectName\\":\\"xining\\",\\"projectNumber\\":\\"\\",\\"projectAlias\\":\\"\\"}","serviceCode":"projectInfo","timestamp":"","uuid":""}`,
        }).then(res => {
          commit("setProjectInfo", res.data.parameter);
          resolve();
        })
      })
    }
  },
  modules: {}
})